﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using uComponents.Core.DataTypes.UrlPicker.Dto;
using umbraco.MacroEngines;
using umbraco.MacroEngines.Library;

namespace uComponents.RazorModels.UrlPicker
{
    [RazorDataTypeModel("22C6F52C-5D41-45B8-845E-C16354CFDD7B")]
    public class UrlPickerModelBinder : IRazorDataTypeModel
    {
        public bool Init(int CurrentNodeId, string PropertyData, out object instance)
        {
            RazorUrlPickerState state = null;

            if (!string.IsNullOrEmpty(PropertyData))
            {
                UrlPickerState innerState = UrlPickerState.Deserialize(PropertyData);
                state = new RazorUrlPickerState()
                            {
                                Mode = innerState.Mode,
                                NodeId = innerState.NodeId,
                                NewWindow = innerState.NewWindow,
                                Title = innerState.Title,
                                Url = innerState.Url
                            };
            }

            instance = state;

            return true;
        }
    }
}
